package encheres.ihm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 *
 * @author Lisi
 */
public class ListeNoireModifier extends javax.swing.JFrame {

    /** Creates new form listeNoireModifier */
    public ListeNoireModifier() {
    			initComponents();
                this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listeNoireModifieur = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        listeNoireModifieur.setColumns(20);
        listeNoireModifieur.setRows(5);
        jScrollPane1.setViewportView(listeNoireModifieur);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
      initText();
    }//GEN-LAST:event_formWindowOpened

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        saveText();        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

   
    private void initText(){

        try{

                Scanner sc = new Scanner(new File("./listeNoire.txt"));
                    while (sc.hasNextLine())
                {
                        String s = sc.nextLine();
                        System.out.println(s);
                        listeNoireModifieur.append(s);
                        listeNoireModifieur.append("\n");
                   }
			
                            
        sc.close();
                                         
        }
		catch(Exception e){
			saveText();
		}


    }

    private void saveText(){
   try
        {                
       
                        String nomFichier = "listeNoire.txt";
                        // Creation du flux de sortie sur le fichier
                        //
                        FileWriter lu = new FileWriter(nomFichier);

                        // Creation du buffer sur le flux de sortie
                        //
                        BufferedWriter bufferedWriter = new BufferedWriter(lu);

                        String ligne = new String();

                        ligne=listeNoireModifieur.getText();
                       

                                // Ecriture de la ligne dans le fichier
                                //
                        bufferedWriter.write(ligne);
                        bufferedWriter.write("\r\n");
                        

                        // Fermeture du buffer sur le flux
                        //
                        bufferedWriter.close();

                        // Fermeture du flux
                        //
                        lu.close();
        }
        catch (Exception e)
        {
            System.out.println("Erreur d'écriture");
        }

       
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea listeNoireModifieur;
    // End of variables declaration//GEN-END:variables

}
